<?php
require_once (__DIR__.DIRECTORY_SEPARATOR.'WechatSDK'.DIRECTORY_SEPARATOR.'Wechat.php');

/**
 * 
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Wechat   
{
    
   private $params;
   private $Wechat;
   public function __construct($Wechat)
    {
        $this->Wechat = $Wechat;
    }
    public function access()
    {
//        $result = $this->Wechat->Validate->checkSignature($_GET);
//        if(!$result)
//        {
//            exit('unauthorized access-!');
//        }
//        if(isset($_GET['echostr']))//验证接口地址有效性
//        {
//            exit($_GET['echostr']);
//        }
//        if ($_SERVER['REQUEST_METHOD'] == "POST") 
//        {
//            $data = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : '' ;
            $postStr = file_get_contents("php://input");
            $postStr = '<xml><ToUserName><![CDATA[gh_a5d55f3c61d0]]></ToUserName>
<FromUserName><![CDATA[oYVyYw2BzLX8eJ1I-oCDSN6z4avI]]></FromUserName>
<CreateTime>1496892450</CreateTime>
<MsgType><![CDATA[location]]></MsgType>
<Location_X>22.541443</Location_X>
<Location_Y>113.932794</Location_Y>
<Scale>15</Scale>
<Label><![CDATA[深圳市南山区南海大道比克科技大厦(腾讯大厦西北)]]></Label>
<MsgId>6429104118912206652</MsgId>
</xml>'
;

            log_result('postStr', $postStr);
            $array = $this->Wechat->Receive->setRevData($postStr);
            log_result('array', $array);
            $result = $this->dispatchMsg();
            echo $result;
            log_result('response', $result);
//        }
    }
    private function dispatchMsg()
    {
        $result  = '';
        switch ($this->Wechat->Receive->getMsgType())
        {
            case 'text':
                $result = $this->Wechat->ReplyMessage->text($this->Wechat->ReplyMessage->getContent())->send();
                break;
            case 'image':
                $result = $this->Wechat->ReplyMessage->image($this->Wechat->ReplyMessage->getMediaID())->send();
                break;
            case 'event'://消息类型是事件
                switch ($this->Wechat->Receive->getEvent())//具体是哪类事件
                {
                    case 'CLICK':
                        switch ($this->Wechat->Receive->getEventKey())//点击事件分多种
                        {
                            case 'V1001_NEWS':
                                $result = $this->sendNews(1);
                                break;
                            case 'V1002_NEWS':
                                $result = $this->sendNews(2);
                                break;
                         }
                        break;
                    case 'scancode_push'://公众号推送但不接收消息
                    case 'scancode_waitmsg':
                        $scanInfo = $this->Wechat->Receive->getScanInfo();
                        $msg = '[Event]:'.$this->Wechat->Receive->getEvent()."\n".'[EventKey]: '.$this->Wechat->Receive->getEventKey()."\n". '[ScanType]: '.$scanInfo['ScanType']."\n". '[ScanResult]: '.$scanInfo['ScanResult'];
                        $result = $this->Wechat->ReplyMessage->text($msg)->send();
                        break;
                    case 'unsubscribe'://公众号推送但不接收消息
                    case 'subscribe':
                        $msg = "感谢关注";
                        $result = $this->Wechat->ReplyMessage->text($msg)->send();
                        break;
                    
                }
                break;
            case 'location':
                $locationInfo = $this->Wechat->Receive->getLocation();
                $msg = '[x]:'.$locationInfo['x']."\n".'[y]: '.$locationInfo['y']."\n". '[scale]: '.$locationInfo['scale']."\n". '[label]: '.$locationInfo['label'];
                $result = $this->Wechat->ReplyMessage->text($msg)->send();
                break;
        }
        return $result;
    }
    /**
     * 发送图文
     * @param type $num
     * @return type
     */
    private function sendNews($num=1)
    {
        $data_1 = array(
                    array(
                        'Title'=>'图文消息标题',
                        'Description'=>'图文消息描述',
                        'PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/qoZrL1JRfwto4DoyXlEhdndglwZMXfI4E9hu8NNkCxSqtQPvxS7klQFFLeQJJhID39V1eoY49iaiaX4u0T6qnaHQ/0',
                        //http://mmbiz.qpic.cn/mmbiz_jpg/qoZrL1JRfwto4DoyXlEhdndglwZMXfI4paHMq9ctfsvJx1anpOhmibDm9sUicQnIqWb9OhoBlwbfrzOpGoNNYttg/0
                        'Url'=>'https://www.baidu.com/',
                    )
                );
        $data_2 = array(
                array(
                    'Title'=>'图文消息标题',
                    'Description'=>'图文消息描述',
                    'PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/qoZrL1JRfwto4DoyXlEhdndglwZMXfI4mDicicGVvjpvItTwSmlVNllpaISrfYlMyqGBwibicQobxshofHia4XDCayg/0',
                    'Url'=>'https://www.huxiu.com//',
                ),
                array(
                    'Title'=>'图文消息标题',
                    'Description'=>'图文消息描述',
                    'PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/qoZrL1JRfwto4DoyXlEhdndglwZMXfI4paHMq9ctfsvJx1anpOhmibDm9sUicQnIqWb9OhoBlwbfrzOpGoNNYttg/0',
                    'Url'=>'http://image.baidu.com/search/wiseala?tn=wiseala&iswiseala=1&ie=utf8&wiseps=1&word=%E9%AB%98%E5%9C%86%E5%9C%86',
                ),
            array(
                    'Title'=>'图文消息标题',
                    'Description'=>'图文消息描述',
                    'PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/qoZrL1JRfwto4DoyXlEhdndglwZMXfI4paHMq9ctfsvJx1anpOhmibDm9sUicQnIqWb9OhoBlwbfrzOpGoNNYttg/0',
                    'Url'=>'http://www.weipai.cn/index.do',
                )
            );
        $data = $num ==1 ? $data_1 : $data_2;
        $result = $this->Wechat->ReplyMessage->news($data)->send(FALSE);
        return $result;
    }
     public function __call($name, $arguments)
    {
        exit("Test does not have [{$name}] method !");
    }
//index, view, create, update, delete, options
}